class DatfileController < ApplicationController
  
  def make
    logger.debug("port==>"+params[:port]+"    session_id=>"+params[:identifier])
    #ucs2=UserConnection.find(:all,:conditions=>["session_id =:session_id and thetime > :time",{:session_id=>params[:identifier],:time=>6.minutes.ago}])
    ucs2=UserConnection.find(:all,:conditions=>["session_id =:session_id ",{:session_id=>params[:identifier]}])
    if(ucs2.nil?) then redirect_to :controller=>"index" end
    ucs=Array.new
    
    ucs2.each {|u|u.swarm_id
      #to update the gateway's ip
      if u.ip!=request.remote_ip then
      	u.ip=request.remote_ip
      	u.save
      end
      ucs+=UserConnection.find(:all,:conditions=>["swarm_id =:swarm_id and thetime > :time",{:swarm_id=>u.swarm_id,:time=>6.minutes.ago}],:group=>"ip")
    }
    addresses=Array.new
    addresses_new=Array.new
    addresses << InetAddressRange.new("0.0.0.0","9.255.255.255") << InetAddressRange.new("11.0.0.0","192.167.255.255") << InetAddressRange.new("192.169.0.0","255.255.255.255")
    ucs.each {|u|u.ip
      
      addresses.each_index {|addr|
        
        if(addresses[addr].isInRange(u.ip))
          puts u.ip
          addresses_new = addresses[0..(addr-1)]+ 
          [ InetAddressRange.new(addresses[addr].startpoint.ip,changeone(u.ip,-1)),InetAddressRange.new(changeone(u.ip,1),addresses[addr].endpoint.ip)]+
          addresses[addr+1..addresses.length]
          addresses=addresses_new
          break
        end
        
      }
      
    }
    
    
    @content=getString(addresses);
    
    
    #    ids.each do |id|
    #
    #      uc2=UserConnection.find(:first,:conditions=>["swarm_id =:swarm_id and ip=:ip and thetime > :thetime",{:swarm_id=>id,:ip=>request.remote_ip,:thetime=>6.minutes.ago}])
    #      if(!uc2.nil?)then uc2.update_attributes(:thetime=>Time.new,:port=>params[:port]) end
    #    end
    indentifier=params[:identifier]
    ucs2=UserConnection.find(:all,:conditions=>["session_id=:session_id ",{:session_id=>indentifier}])
    ucs2.each {|uc2|
      if(!uc2.nil?)then uc2.update_attributes(:thetime=>Time.new,:port=>params[:port]) end
    }
    
    
    @content_type='text/plain'
    @response.headers["Content-Type"] = @content_type
    logger.debug("request.remote_ip => "+request.remote_ip+"time=>"+Time.new.to_s+"vincent messege dat=>\n"+@content+"\n end vicent messege")
    render :layout=>false
    
  end
  
  
  
  def changeone(ip,i)
    parts = ip.split("[.]")
    int_array=RangeMark.new(ip).getTokens
    int_new=getdone(int_array,3,i)
    int_new.join('.')
  end
  def getdone(i,x,add)
    puts i.length,x,add
    if(add==1)
      if(i[x]==255)
        i[x]=0
        getdone(i,x-1,1)
      end
      
      i[x] +=1
    end
    
    if(add==-1)
      if(i[x]==0)
        i[x]=255
        getdone(i,x-1,-1)
      end
      i[x]-=1
    end
    i
  end
  
  def getString(addresses)
    string=String.new
    addresses.each {|a|string << a.startpoint.ip+"-"+a.endpoint.ip+",200,for ban ips\n"}
    
    return string.chomp
  end
end
